#pragma once
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define Path "./fifo"
#define Mode 0666
void CreatPipeFile()
{
    umask(0);
    int n = mkfifo(Path, Mode);
    if(n == -1)
    {
        std::cout << "create pipe file fail" << std::endl;
        std::cerr << "errno : " << errno << ", strerror : " << strerror(errno) << std::endl;
        exit(-1);
    }
}

void DelPipeFile()
{
    unlink(Path);
}

bool read_pipe(int rfd)
{
    char c = 0;
    ssize_t ret = read(rfd, &c, sizeof(c));
    if(ret == -1)
    {
        std::cout << "read pipe file fail" << std::endl;
        std::cerr << "errno : " << errno << ", strerror : " << strerror(errno) << std::endl;
        exit(-1);
    }
    else if(ret == 0)
    {
        return false;
    }
    return true;
}

void write_pipe(int wfd)
{
    char c = 0;
    ssize_t ret = write(wfd, &c, sizeof(c));
    if(ret == -1)
    {
        std::cout << "write pipe file fail" << std::endl;
        std::cerr << "errno : " << errno << ", strerror : " << strerror(errno) << std::endl;
        exit(-1);
    }
}

int open_pipe(int flag)
{
    int ret = open(Path, flag);
    if(ret == -1)
    {
        std::cout << "open pipe file fail" << std::endl;
        std::cerr << "errno : " << errno << ", strerror : " << strerror(errno) << std::endl;
        exit(-1);
    }
    return ret;
}